//
//  DynamicColumnFieldCore+GroupTableView.m
//  mcrmSAAS
//
//  Created by wjxfhxy on 15-12-31.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "DynamicColumnFieldCore+GroupTableView.h"

#import "DynamicColumnFieldCore+Internal.h"

@implementation DynamicColumnFieldCore (GroupTableView)

-(void)keyBoardDidShow:(CGFloat)keyBoardOffset
{
    _gtable.height -= keyBoardOffset;
    
    if (_textInputViewCell != nil)
    {
        [_gtable scrollRectToVisible:_textInputViewCell.frame animated:YES];
    }
}

-(void)keyBoardWillHide:(CGFloat)keyBoardOffset
{
    _gtable.height += keyBoardOffset;
}

-(void)keyBoardFrameWillChange:(CGFloat)changeOffet
{
    _gtable.height += changeOffet;
}

-(void)keyBoardDidShow:(CGFloat)keyBoardOffset scrollToVisibleRect:(CGRect)rect
{
    _gtable.height -= keyBoardOffset;
    
    [_gtable scrollRectToVisible:rect animated:YES];

}

-(ExpandDynamicCell*)expandDynamicCellWithTag:(NSString*)tag
{
    return (ExpandDynamicCell*)[_gtable cellWithTag:tag];
}

-(NSIndexPath*)indexPathForExpandDynamicCell:(ExpandDynamicCell *)cell
{
    return [_gtable indexPathOfCell:cell];
}

-(void)insertExpandDynamicCellsAtIndexPaths:(NSArray*)indexPaths
{
    [_gtable insertRowsAtIndexPaths:indexPaths withRowAnimation:XUIGroupTableViewRowAnimationNone];
}

-(void)reloadExpandDynamicCellsAtIndexPaths:(NSArray*)indexPaths
{
    [_gtable reloadRowsAtIndexPaths:indexPaths withRowAnimation:XUIGroupTableViewRowAnimationNone];
}

-(void)deleteExpandDynamicCellsAtIndexPaths:(NSArray*)indexPaths
{
    [_gtable deleteRowsAtIndexPaths:indexPaths withRowAnimation:XUIGroupTableViewRowAnimationNone];
}

-(void)reloadExpandDynamicCellsAtSections:(NSIndexSet *)sections
{
    [_gtable reloadSections:sections withRowAnimation:XUIGroupTableViewRowAnimationNone];
}

-(CGFloat)contentSizeHeight
{
    return _gtable.contentSize.height;
}

-(void)setHeight:(CGFloat)height
{
    _gtable.height = height;
}

-(void)setScrollEnabled:(BOOL)enabled
{
    _gtable.scrollEnabled = enabled;
}

-(CGFloat)caleHeight
{
    CGFloat totalHeight = 0;
    XMSQueueDictionary *constraint = [_dataSource constraintOfDynamicColumnFieldCore:nil];
    
    NSArray *allKeys = [constraint allKeys];
    for(NSUInteger i = 0;i < allKeys.count;i++)
    {
        if([_delegate respondsToSelector:@selector(dynamicColumnFieldCore:heightForHeaderInGroup:)])
            totalHeight += [_delegate dynamicColumnFieldCore:nil heightForHeaderInGroup:i];
        else
            totalHeight += 26;
        
        NSArray *values = [constraint getValueForIndex:i];
        
        for(NSUInteger j = 0;j < values.count;j++)
        {
            FieldDescript *fieldDescript = values[j];
            CGFloat height = 0; //[_delegate dynamicColumnFieldCore:nil dynamicColumnCellWillCaleHeight:fieldDescript];
            if(height != 0)
            {
                totalHeight += height;
            }
            else
            {
                if(fieldDescript.controlType.intValue == DynamicFieldControlTypeStaticText||
                   fieldDescript.controlType.intValue == DynamicFieldControlTypeTextSingleSelect||
                   fieldDescript.controlType.intValue == DynamicFieldControlTypeMultiSelectStaticText||
                   fieldDescript.controlType.intValue == DynamicFieldControlTypeDateTimeStaticText||
                   fieldDescript.controlType.intValue == DynamicFieldControlTypeStaticImagePanel||
                   fieldDescript.controlType.intValue == DynamicFieldControlTypeStaticImagePanel)
                {
                    id value = [self valueForFieldDescript:fieldDescript];
                    value = [self formatValue:value fieldDescript:fieldDescript];
                    totalHeight +=  [ExpandDynamicCellFactory caleStaticText:fieldDescript.controlType.intValue
                                                              heightWithText:value
                                                              forContainWidth:_gtable.width];
                }
                else
                {
                    totalHeight += [ExpandDynamicCellFactory caleHeght:fieldDescript];
                }
            }
        }
    }
    
    return totalHeight;
}

-(CGRect)converTextInputViewRectToView:(UIView*)toView
{
    if(_textInputViewCell == nil)
        return CGRectZero;
    
    return [_gtable convertRect:_textInputViewCell.frame toView:toView];
}

@end
